﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using HalconDotNet;

namespace ImageControl
{
    public class DrawTool
    {
        private HDrawingObject selectedObject = new HDrawingObject(250, 250, 100);//用来记录选中对象
        public DrawTool()
        {
            DrawingItems = new ObservableCollection<DrawingItem>();
        }
        public DrawingItem this[int i]
        {
            get { return DrawingItems[i]; }
        }

        public DrawingItem this[string name]
        {
            get { return DrawingItems.FirstOrDefault(p => p.Name == name); }
        }
        public ObservableCollection<DrawingItem> DrawingItems { get; private set; }
        public void DrawItem(string drawingType, string name, string color)
        {
            DrawingType type = (DrawingType)Enum.Parse(typeof(DrawingType), drawingType);
            DrawItem(type, name, color);
        }

        public void DrawItem(DrawingType drawingType, string name, string color)
        {

            HDrawingObject drawingObject = null;
            switch (drawingType)
            {
                case DrawingType.Rectangle1:
                    drawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE1, 100, 100, 210, 210);
                    break;
                case DrawingType.Rectangle2:
                    drawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE2, 100, 100, 0, 100, 50);
                    break;
                case DrawingType.Circle:
                    drawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE, 200, 200, 70);
                    break;
                case DrawingType.Ellipse:
                    drawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.ELLIPSE, 50, 50, 0, 100, 50);
                    break;
                case DrawingType.Arc:
                    drawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE_SECTOR, 100, 100, 80, 0, 0.7);
                    break;
            }
            if (drawingObject != null)
            {
                drawingObject.SetDrawingObjectParams("color", color);
                this.DrawingItems.Add(new DrawingItem(name, color, drawingType, drawingObject));
            }
        }

        public void AttachAllDrawObj(HWindow hWindow)
        {
            foreach (var item in DrawingItems)
            {
                if (hWindow != null && hWindow.IsInitialized())
                {
                    AttachDrawObj(hWindow, item.HDrawingObject);
                }
            }
            AttachDrawingObjects(hWindow);
        }

        public void AttachDrawObj(HWindow hWindow, HDrawingObject obj)
        {
            //------下面连续4个注册回调方法---------------------------------------------------
            //obj.OnDrag(visionTool.DrawObjCallback);//拖拽回调
            //obj.OnAttach(visionTool.DrawObjCallback);//附加到窗口时回调
            //obj.OnResize(visionTool.DrawObjCallback);//尺寸改变时回调
            obj.OnSelect(OnSelectDrawingObject);//选中时回调
            if (selectedObject == null)//更新当前选择---直接选
                selectedObject = obj;
            hWindow.AttachDrawingObjectToWindow(obj);//把绘制图像附加到窗体
        }
        private void OnSelectDrawingObject(HDrawingObject dobj, HWindow hwin, string type)
        {
            selectedObject = dobj;
        }

        /// <summary>
        /// 隐藏绘图对象
        /// </summary>
        public void DetachDrawObjects(HWindow window)
        {
            foreach (DrawingItem item in this.DrawingItems)
            {
                window.DetachDrawingObjectFromWindow(item.HDrawingObject);
            }
        }

        /// <summary>
        /// 显示绘图对象
        /// </summary>
        public void AttachDrawingObjects(HWindow window)
        {
            foreach (DrawingItem item in this.DrawingItems)
            {
                window.AttachDrawingObjectToWindow(item.HDrawingObject);
            }
        }
        public void DeleteSelectObject(HWindow window, HDrawingObject selectObj)
        {
            if (selectObj != null)
            {
                long temp = selectObj.ID;//记录id
                var result = DrawingItems.First(item => item.HDrawingObject.ID == selectObj.ID);
                if (result.HDrawingObject != null && result.HDrawingObject.IsInitialized())
                {
                    window.DetachDrawingObjectFromWindow(result.HDrawingObject);//从Halcon窗口中分离现有绘图对象
                    result.HDrawingObject.Dispose();
                }
                DrawingItems.Remove(result);
                selectObj.Dispose();//释放内存
                selectObj = null;
            }
        }

        public void DeleteAllObject(HWindow window)
        {
            foreach (var result in this.DrawingItems)
            {
                if (result.HDrawingObject != null && result.HDrawingObject.IsInitialized())
                {
                    window.DetachDrawingObjectFromWindow(result.HDrawingObject);//从Halcon窗口中分离现有绘图对象
                    result.HDrawingObject.Dispose();
                }
            }
            DrawingItems.Clear();
        }

        public void Save(XmlDocument xmlDoc, XmlElement node)
        {
            foreach (var tool in DrawingItems)
            {
                XmlElement drawNode = xmlDoc.CreateElement("DrawTool");
                node.AppendChild(drawNode);
                tool.Save(drawNode);
            }
        }

        public void Load(XmlElement node)
        {
            foreach (XmlElement xe in node.ChildNodes)
            {
                string name = xe.Attributes["Name"].Value;
                var item = DrawingItems.FirstOrDefault(p => p.Name == name);
                if (item != null)
                {
                    item.Load(xe);
                }
            }
        }

    }
}
